// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    list: [],
    all: 'all',
    isloading: false,
    total: '', // 总共条数
    per_page: 4, // 每页条数
    last_page: 1, // 最后一页
    current_page: 1, // 当前页
  },
  onLoad: function (options) {
    var that = this;
    // 判断传参是否为空
    if (Object.keys(options).length === 0) {
      this.setData({
        currentTab: 0
      })
    } else {
      this.setData({
        currentTab: options.currentTab
      })
    }
    console.log(this.data.currentTab);
    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });

    if (this.data.currentTab == 0) {
      this.list(this.data.all)
    } else {
      this.list(this.data.currentTab)
    }

  },
  list(status) {
    var that = this
    wx.request({
      url: `${baseurl}/order.Order/order_list`,
      method: 'POST',
      data: {
        user_id: wx.getStorageSync("user_id"),
        status: status,
        page: 1,
        total: '', // 总共条数
        per_page: 4, // 每页条数
        last_page: 1, // 最后一页
        current_page: 1, // 当前页
      },
      success: function (res) {
        console.log("131313", res.data);
        if (res.data.code == 600) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }, 500);

        }
        that.setData({
          list: res.data.data.data,
          current_page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          per_page: res.data.data.per_page,
          total: res.data.data.total
        })
      }
    })
  },
  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (this.data.currentTab == 0) {
      this.list(this.data.all)
    } else {
      this.list(this.data.currentTab)
    }
  },

  bindChange: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },
  onShow: function () {
    if (this.data.currentTab == 0) {
      this.list(this.data.all)
    } else {
      this.list(this.data.currentTab)
    }
  },
  // 触底加载
  onReachBottom: function () {
    // console.log('2222222344');
  
  },
  // 触底加载
  getMore1(that) {
    // wx.showToast({
    //   title: '触底了',
    //   icon: 'none',
    //   duration: 2000
    // })
    that.setData({
      isloading: true
    })
    var page = that.data.current_page; //页码
    if (that.data.last_page == page) {
      wx.showToast({
        title: '没有更多数据！',
        icon: 'none',
        duration: 2000
      })
    } else {
      page++;
      this.setData({
        current_page: page
      })
      // console.log(page);
      // 需求列表

      if (this.data.currentTab == 0) {
        wx.request({
          url: `${baseurl}/order.Order/order_list`,
          method: 'POST',
          data: {
            user_id: wx.getStorageSync("user_id"),
            status: this.data.all,
            page: page, // 当前页
          },
          success: (res) => {
            // console.log('2222', res.data.data);
            this.setData({
              list: [...this.data.list, ...res.data.data.data],
              current_page: res.data.data.current_page,
              last_page: res.data.data.last_page,
              per_page: res.data.data.per_page,
              total: res.data.data.total
            })
            console.log(this.data.list);
          }
        })
      } else {
        wx.request({
          url: `${baseurl}/order.Order/order_list`,
          method: 'POST',
          data: {
            user_id: wx.getStorageSync("user_id"),
            status: this.data.currentTab,
            page: page, // 当前页
          },
          success: (res) => {
            // console.log(res.data.data);
            this.setData({
              list: [...this.data.list, ...res.data.data.data],
              current_page: res.data.data.current_page,
              last_page: res.data.data.last_page,
              per_page: res.data.data.per_page,
              total: res.data.data.total
            })
            // console.log(this.data.list);
          }
        })
      }

    }

  },
  //底部触发
  //  onReachBottom: function () {
  //   wx.showLoading({
  //     title: '加载中...',
  //   })
  //  console.log('到底了');
  // },


  //存储y轴改变值
  //  this.dyAry=[];
  //eventhandle swiper-item 的位置发生改变时会触发
  scrollbottom(e) {
    console.log('123456123456123456123456123456123456123456123456123456123456123456123456123456123456123456123456');
    var that = this;
    this.getMore1(that)
  }

})